//
//  StoreDetailOpusCatCell.m
//  BaseProject
//
//  Created by 王洋 on 2020/9/20.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "ZZLayout.h"

#import "SearchKeyWordItemCell.h"
#import "StoreDetailOpusCatCell.h"

@interface StoreDetailOpusCatCell ()<UICollectionViewDelegate, UICollectionViewDataSource, ZZLayoutDelegate>


@end

@implementation StoreDetailOpusCatCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self layout];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)fillCellWithArray:(NSMutableArray<CommonOpusCatModel *>*)dataArray {
    self.dataArray = @[].mutableCopy;
    self.dataArray = dataArray;
    
    [self.collectionView reloadData];
}


- (void)layout {
    
    [self addSubview:self.collectionView];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
    }];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SearchKeyWordItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SearchKeyWordItemCellID" forIndexPath:indexPath];
    cell.itemTitleLab.text = self.dataArray[indexPath.row].opusClassName;
    
    if (self.dataArray[indexPath.row].isSelect) {
        cell.itemTitleLab.backgroundColor = rgba(255, 242, 233, 1);
        cell.itemTitleLab.textColor = rgba(254, 118, 15, 1);
    } else {
        cell.itemTitleLab.backgroundColor = rgba(241, 242, 244, 1);
        cell.itemTitleLab.textColor = rgba(47, 47, 52, 1);

    }
    
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.dataArray[indexPath.row].itemWidth, 30);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
        if (self.StoreDetailOpusCatCellBlock) {
            self.StoreDetailOpusCatCellBlock(indexPath.row);
        }
}

#pragma mark- 懒加载
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        //注册cell
        [_collectionView registerNib:[UINib nibWithNibName:@"SearchKeyWordItemCell" bundle:nil] forCellWithReuseIdentifier:@"SearchKeyWordItemCellID"];
        
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.bounces = YES;
//        self.edgesForExtendedLayout = UIRectEdgeNone;
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _collectionView.backgroundColor = [UIColor whiteColor];
        
    }
    return _collectionView;
}


@end
